<?php
	require 'global.php';
	require './database/common.php';
	
	$db_config = require ROOT.'/tool/database/data.php';
	if(isset($_GET['admin']) && $_GET['admin'] == ''){
		$admin = true;
	}else{
		$admin = false;
	}
	if(isset($_GET['refresh']) && $_GET['refresh']==1){
		$version = time();
		$db_config['version'] = $version;
		
		$sql = "SELECT * FROM information_schema.TABLES where table_schema = 'ecmall'";
		$data = getAll($sql);
		foreach($data as $db_info){
			if(!array_key_exists($db_info['TABLE_NAME'], $db_config['tables']) || !$db_config['tables'][$db_info['TABLE_NAME']]['exists'])
				$db_config['tables'][$db_info['TABLE_NAME']]['exists'] = false;
				
			if(!empty($db_info['TABLE_COMMENT']) && empty($db_config['tables'][$db_info['TABLE_NAME']]['description']))
				$db_config['tables'][$db_info['TABLE_NAME']]['description'] = $db_info['TABLE_COMMENT'];
			$db_config['tables'][$db_info['TABLE_NAME']]['engine'] = $db_info['ENGINE'];
			$db_config['tables'][$db_info['TABLE_NAME']]['table_rows'] = $db_info['TABLE_ROWS'];
			$db_config['tables'][$db_info['TABLE_NAME']]['data_length'] = $db_info['DATA_LENGTH'];
			$db_config['tables'][$db_info['TABLE_NAME']]['index_length'] = $db_info['INDEX_LENGTH'];
			$db_config['tables'][$db_info['TABLE_NAME']]['create_time'] = $db_info['CREATE_TIME'];
			$db_config['tables'][$db_info['TABLE_NAME']]['update_time'] = $db_info['UPDATE_TIME'];
			$db_config['tables'][$db_info['TABLE_NAME']]['collation'] = $db_info['TABLE_COLLATION'];
			$db_config['tables'][$db_info['TABLE_NAME']]['version'] = $version;
			$sql = "SELECT * FROM information_schema.columns WHERE table_schema = 'ecmall' AND table_name='{$db_info['TABLE_NAME']}'";
			$fs = getAll($sql);
			foreach ($fs as $f){
				if(!array_key_exists($f['COLUMN_NAME'], $db_config['tables'][$db_info['TABLE_NAME']]['fields']) || 
					!$db_config['tables'][$db_info['TABLE_NAME']]['fields'][$f['COLUMN_NAME']]['exists'])
					$db_config['tables'][$db_info['TABLE_NAME']]['fields'][$f['COLUMN_NAME']]['exists'] = false;
				$db_config['tables'][$db_info['TABLE_NAME']]['fields'][$f['COLUMN_NAME']]['type'] = $f['DATA_TYPE'];
				$db_config['tables'][$db_info['TABLE_NAME']]['fields'][$f['COLUMN_NAME']]['length'] = $f['NUMERIC_PRECISION'];
				$db_config['tables'][$db_info['TABLE_NAME']]['fields'][$f['COLUMN_NAME']]['nullable'] = $f['IS_NULLABLE']=='YES';
				$db_config['tables'][$db_info['TABLE_NAME']]['fields'][$f['COLUMN_NAME']]['auto_incr'] = $f['EXTRA'];
				$db_config['tables'][$db_info['TABLE_NAME']]['fields'][$f['COLUMN_NAME']]['pk'] = $f['COLUMN_KEY']!=null &&$f['COLUMN_KEY']=='PRI';
				$db_config['tables'][$db_info['TABLE_NAME']]['fields'][$f['COLUMN_NAME']]['default_value'] = $f['COLUMN_DEFAULT'];
				if(!empty($f['COLUMN_COMMENT']) && empty($db_config['tables'][$db_info['TABLE_NAME']]['fields'][$f['COLUMN_NAME']]['description']))
					$db_config['tables'][$db_info['TABLE_NAME']]['fields'][$f['COLUMN_NAME']]['description'] = $f['COLUMN_COMMENT'];
	//			$db_config['tables'][$db_info['TABLE_NAME']]['fields'][$f['COLUMN_NAME']]['name'] = '';
	//			$db_config['tables'][$db_info['TABLE_NAME']]['fields'][$f['COLUMN_NAME']]['indexed'] = '';
	//			$db_config['tables'][$db_info['TABLE_NAME']]['fields'][$f['COLUMN_NAME']]['fk']['type'] = '';
	//			$db_config['tables'][$db_info['TABLE_NAME']]['fields'][$f['COLUMN_NAME']]['fk']['table'] = '';
	//			$db_config['tables'][$db_info['TABLE_NAME']]['fields'][$f['COLUMN_NAME']]['fk']['field'] = '';
				$db_config['tables'][$db_info['TABLE_NAME']]['fields'][$f['COLUMN_NAME']]['version'] = $version;
			}
		}
		save_data_config($db_config);
	}
	
	//组装分类下属
	foreach ($db_config['tables'] as $table_name => &$table) {
		if(array_key_exists('module', $table)){
			foreach ($table['module'] as $m){
				$db_config['module'][$m]['children'][$table_name] = $table;
			}
		}else{
			$db_config['module']['other']['children'][$table_name] = $table;
		}
	}
	require 'header.php';
?>
<style>
	.accordion a{display:block;color:#333;border;1px solid #EEE;padding-left:10px;}
	.accordion a:hover{border;1px solid #999;background:#1C94C4;color:white;}
	.detail_list_table,.detail_table{width:100%;}
	.detail_list_table th,.detail_table th{padding:auto 5px;background:#333;color:white;}
	.detail_list_table td,.detail_table td{padding:3px 5px;background:white;}
	.detail_list_table tr:hover td,.detail_table tr:hover td{background:#FDF5CE}
	.detail_list_table tr td a,.detail_table tr td a{color:#333;}
	.detail_list_table tr td a:hover,.detail_table tr td a:hover{text-decoration: underline;}
	.detail_list_table tr.title,.detail_table tr.title{position:fixed;width: 100%;}
	.center{text-align: center;}
	.right{text-align: right;}
	#setting{display:none;}
	.description{border:1px dotted #666; background: white;color:#1C94C4;padding:5px 10px;margin:0;}
</style>
		<div id="main_nav">
			<h2><a href="javascript:void(0);">数据库检查</a></h2>
		</div>
		<div id="left_nav">
			<h2>数据库文档</h2>
			<div>
				<ul class="accordion">
				<?php
				$cur_accordion = null;
				foreach($db_config['module'] as $key=>$w){
					if(empty($cur_accordion))
						$cur_accordion = $key;
				?>
					<li id="module_<?php echo $key?>" module="<?php echo $key?>">
						<h3><?php echo $w['name']?></h3>
						<div>
							<?php if(isset($w['description'])){?>
							<p class="description">
								<?php echo $w['description']?>
							</p>
							<?php 
								}
								foreach($w['children'] as $n=>$t){
							?>
							<a href="javascript:load_table_info('<?php echo $n ?>')"><?php echo '['.$n.']'.$t['name'] ?></a>
							<?php }?>
						</div>
					</li>
				<?php }?>
				</ul>
			</div>
		</div>
		<div id="main_content">
			<div id="dialog" title="数据表-ALL">
				<table class="detail_list_table" cellspacing="1">
					<tr class="title">
						<th>表名</th>
						<th>模块</th>
						<th>记录数</th>
						<th>数据大小</th>
						<th>索引大小</th>
						<th>字段</th>
						<th>备注</th>
					</tr>
					<tr><td colspan="7" id="mt"></td></tr>
				<?php 
				foreach ($db_config['tables'] as $table_name => $table) {
				?>
				<tr table="<?php echo $table_name ?>">
					<td>
						<a href="javascript:load_table_info('<?php echo $table_name ?>')">[<?php echo $table_name;?>] <?php echo $table['name']?></a>
					</td>
					<td>
					<?php 
					if(array_key_exists('module', $table)){
						foreach ($table['module'] as $m){
							$db_config['module'][$m];
							echo '<a href="javascript:load_table_info(\'module:'. $m .'\')">'.$db_config['module'][$m]['name'].'</a>　';
						}
					}else{
						echo '<a href="javascript:load_table_info(\'module:other\')">'.$db_config['module']['other']['name'].'</a> ';
					}
					?>
					</td>
					<td class="right"><?php echo $table['table_rows']?></td>
					<td class="right"><?php echo $table['data_length']?></td>
					<td class="right"><?php echo $table['index_length']?></td>
					<td class="right"><?php echo sizeof($table['fields']) ?></td>
					<td><?php echo $table['description']?></td>
				</tr>
				<?php 
				}
				?>
				</table>
			</div>
		</div>
		<div id="setting">loading...</div>
		<div id="table_dia" style="display:none;" title="表">Loading...</div>
		<div id="table_em">
			
		</div>
		<script type="text/javascript" language="javascript" src="/includes/libraries/javascript/jquery.plugins/jquery.contextmenu.r2.packed.js"></script>
		<script type="text/javascript" language="javascript">
			$(function() {
				$( ".accordion" ).accordion({active:"#module_<?php echo $cur_accordion?>",autoHeight: false,
					changestart: function(event, ui) {
					var name = ui.newHeader.parent().attr("module");
					load_table_info("module:"+name);
				}});
				
				//load_widget_info("<?php echo $widget_data[$cur_accordion]['widget_name']?>",
				//	{position: ['right','bottom'] ,height:$(window).height()-118,width:$("#main_content").width()});
				
				<?php if(!empty($id)){?>
				//如果id存在，则展开ID
				setTimeout(function(){
					$("#widget_<?php echo $id?>").click();
				},500);
				<?php }?>

				$("#new_widget").click(function(){
					$("#widget_add_dialog").dialog({height:300,width:600,modal: true});
				});

				$("#widget_update").click(function(){
					load_updater({height:300,width:600,modal: true});
				});

				load_table_info();

				$("[table]").dblclick(function(){
					var table_name = $(this).attr("table");
					$("#setting").dialog({position: ['center','center'] ,height:$(window).height()-118,width:$("#main_content").width(),title : "数据表设置 - "+table_name,open:function(){
						$("#setting").load(url,function(){
							
						});
					}});
				});
			});

			function load_table_info(name){
				var title = "WAP3 - 数据库";
				var url = null;
				
				if(name!=null && name != ''){
					$("#dialog").html("loading...");
					var index = name.indexOf(":");
					if(index==-1){
						//表明细
						title += " - 数据表 - " + name;
						url = "/tool/database/table_info.php?t="+name;
					}else{
						//分类列表
						name = name.substr(index+1);
						title += " - 模块 - " + name;
						url = "/tool/database/module_info.php?m="+name;
					}
				}else{
					title += " - ALL";
				}
					
				$("#dialog").dialog({position: ['right','bottom'] ,height:$(window).height()-118,width:$("#main_content").width(),title : title,open:function(){
					format_list_table();
				}});
				//$("#dialog").dialog("option","title",title);
				if(url!=null){
					$("#dialog").load(url,function(){
						$(".tabs").tabs({selected:1});
						format_list_table();
					});
				}
			}
			
			function format_list_table(){
				$(".detail_list_table tr:eq(2) td").each(function(i){
					var w = $(this).width();
					var wp = parseInt($(this).css('padding-left')) + parseInt($(this).css('padding-right'));
					$(".detail_list_table tr.title th:eq("+i+")").css({width:w + wp-2});
				});
				$("#mt").css({'height': $(".detail_list_table tr.title th:eq(1)").height()})
			}
			
			function edit_table(table_name){
				$("#table_dia").dialog({title:"表: " + table_name + " 修改",height:300,width:600,modal: true});
				$("#table_dia").load("/tool/database/table_edit.php?name=" + table_name);
			}
	</script>
	<?php 
		require 'footer.php';
	?>